package com.bsit.obu.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;


import com.bsit.obu.R;
import com.bsit.obu.utils.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CarLenthDialog extends Dialog {

	@BindView(R.id.win_prompt_title)
	protected TextView titleView;
	@BindView(R.id.car_lenth)
	protected EditText carLenth;
	@BindView(R.id.car_width)
	protected EditText carWidth;
	@BindView(R.id.car_height)
	protected EditText carHeigh;


	@BindView(R.id.tag1)
	TextView tag1;
	@BindView(R.id.tag2)
	LinearLayout tag2;
	@BindView(R.id.tag3)
	LinearLayout tag3;
	@BindView(R.id.tag4)
	TextView tag4;
	@BindView(R.id.public_ll)
	LinearLayout publicLL;
	@BindView(R.id.spinner)
	Spinner spinner;









	private String title;
	private String content;
	protected View view;
	protected onSubmitListener onSubmit;
	Context context;
	private boolean isUpload;
	private String imageTypeStr;

	public CarLenthDialog(Context context, String title, String content,boolean picUpLoad,
						  onSubmitListener onSubmit) {
		super(context, R.style.dialog);
		this.context = context;
		this.content = content;
		this.title = title;
		this.onSubmit = onSubmit;
		this.isUpload = picUpLoad;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = inflater.inflate(R.layout.dialog_carlenth, null);
		ButterKnife.bind(this, view);


		if(isUpload){
			ArrayList<String> list = new ArrayList();
			list.add("请选择图片上传类型");
			list.add("二次发行上传图片");
			list.add("信息变更上传图片");
			ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item,list);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner.setAdapter(adapter);
			tag1.setText("操作员编号：");
			tag2.setVisibility(View.GONE);
			tag3.setVisibility(View.GONE);
			tag4.setText("上传图片的类型：");
			spinner.setPrompt("请选择图片上传的类型");
			spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
										   int pos, long id) {
					Log.e("Log", "pos is : " + pos + "  pos is : " + id);
					if (pos == 1 || pos == 2){
						imageTypeStr = String.valueOf(pos-1);
					}
				}
				@Override
				public void onNothingSelected(AdapterView<?> parent) {
					// Another interface callback
				}
			});
			carLenth.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
			carWidth.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});
			carHeigh.setFilters(new InputFilter[]{new InputFilter.LengthFilter(4)});
		} else {
			publicLL.setVisibility(View.GONE);
			imageTypeStr = "0";
			carLenth.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
			carWidth.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
			carHeigh.setFilters(new InputFilter[]{new InputFilter.LengthFilter(3)});
		}
//		publicLL.setVisibility(View.GONE);



		if (title != null && !title.equals(""))
			titleView.setText(title);

		/*if (content != null && !content.equals(""))
			contentView.setText(content);*/
		this.setContentView(view);
	}

	@OnClick(R.id.win_prompt_submit)
	protected void submit(){
		String carLenthStr = carLenth.getText().toString().trim();
		String carWidthStr = carWidth.getText().toString();
		String carHeightStr = carHeigh.getText().toString();
		if (isUpload){
			if (TextUtils.isEmpty(carLenthStr) || TextUtils.isEmpty(imageTypeStr)){
				ToastUtils.showToast(context, "请输入完整的上传信息");
				return;
			}
		} else {
			if (TextUtils.isEmpty(carLenthStr) || TextUtils.isEmpty(carWidthStr) || TextUtils.isEmpty(carHeightStr) || TextUtils.isEmpty(imageTypeStr)){
				ToastUtils.showToast(context, "请输入完整的长宽高信息");
				return;
			}
		}
		onSubmit.onSubmit(carLenthStr, carWidthStr, carHeightStr,imageTypeStr);
		dismiss();
	}



	public interface onSubmitListener{
		void onSubmit(String carLenth, String carWidth, String carHeight, String imaStr);
	}
}
